package com.example.testnews.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.testnews.activity.CollectNewsActivity;
import com.example.testnews.R;
import com.example.testnews.bean.News;

import java.util.ArrayList;
import java.util.List;

public class CollectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private static final String TAG = "CollectAdapter";
    private static OnCollectItemClickListener mCollectListener;
    private List<News> collectList = new ArrayList<>();
    private Context context;

    public CollectAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news,parent,false);
        return new CollectViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        News news = collectList.get(position);

        CollectViewHolder newsViewHolder = (CollectViewHolder) holder;
        Log.d(TAG, "onBindViewHolder: "+news);
        Glide.with(newsViewHolder.mIvIcon.getContext()).load("https:"+news.picUrl).into(newsViewHolder.mIvIcon);

        newsViewHolder.mTvTitle.setText(news.title);
        newsViewHolder.mTvDesc.setText(news.description);
        newsViewHolder.mTvTime.setText(news.time);

        newsViewHolder.mCollectNewsView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, CollectNewsActivity.class);
                intent.putExtra("collect_news",collectList.get(position).url);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return collectList.size();
    }

    public void setCollectNews(List<News> collectList){
        this.collectList=collectList;
        notifyDataSetChanged();
    }

    public interface OnCollectItemClickListener{
        void onClick(int position);
    }

    public static void setOnCollectItemClickListener(OnCollectItemClickListener collectListener){
        mCollectListener = collectListener;
    }

    static class CollectViewHolder extends RecyclerView.ViewHolder{

        public ImageView mIvIcon;
        public TextView mTvTitle;
        public TextView mTvDesc,mTvTime;
        public View mCollectNewsView;


        public CollectViewHolder(View view){
            super(view);
            mIvIcon =view .findViewById(R.id.icon);
            mTvTitle =view .findViewById(R.id.title);
            mTvDesc =view .findViewById(R.id.desc);
            mTvTime =view .findViewById(R.id.time);
            mCollectNewsView = view;

        }
    }
}
